
var file_util = require('../public/file_util');
var path = require('path');
const Define = require('./ExportDefine')
function TSParser(sheet) {

    this.sheet = sheet;
}
TSParser.prototype.getModuleName = function () {
    return this.sheet.getModuleName()
}
TSParser.prototype.parseEnum = function () {
    var sheetName = this.sheet.sheetName
    // console.log(" sheetname", sheetName);

    //生成枚举。
    var enumName = sheetName.substring(0, 1).toUpperCase() + sheetName.substring(1);
    this.fileNames = enumName + ': ' + "'" + this.sheet.sheetName + "',"
    var declareObj = 'declare class Json' + enumName + ' {\n\t%{data}\n}'
    var enumObj = "export enum " + enumName + "Index {\n\t%{data}\n}";
    var enums = "";
    var declares = ''
    // var keyIndex = 0;
    let keyArray = this.sheet.keyArray;
    let typeArray = this.sheet.typeArray;
    let nameArray = this.sheet.nameArray;
    for (var k = 0; k < keyArray.length; k++) {
        if (typeArray[k] && typeArray[k] != 'null') {
            var key = keyArray[k];
            let type = typeArray[k]
            if (key != undefined) {
                let tname = Define.TS_TYPE[type.trim()];
                if (!tname) {
                    console.error(' tname is null type is ', type)
                }
                if (nameArray[k]) {
                    let temp = nameArray[k].replace(/\n/g, '') + "\n\t";
                    enums += key + ",// " + temp;
                    declares += key + ':' + tname + ";// " + temp;
                } else {
                    enums += key + "," + "\n\t";
                    declares += key + ':' + tname + ";" + "\n\t";
                }
                // this.nameList[keyIndex] = key
                // this.typeList[keyIndex] = typeArray[k]
                // if (dbArray && dbArray[k]) {
                //     this.dbList[keyIndex] = dbArray[k]
                // } else {
                //     // console.warn('k is ', k)
                // }
                // this.indexOfKey[key] = keyIndex++
            }

        }
    }
    // console.log(" indexOfKey", this.indexOfKey);
    enumObj = enumObj.replace('%{data}', enums);
    this.enumObj = enumObj;
    this.enumName = enumName;
    // console.log('this.enumObj  ', this.enumObj)

    declareObj = declareObj.replace('%{data}', declares);
    this.declareObj = declareObj;
    // console.log('this.declareObj  ', this.declareObj)
}
TSParser.prototype.parseEnumDefine = function () {
    if (this.sheet.getExportType() != Define.FILE_TYPE_ENUM) {
        return;
    }
    var sheetName = this.sheet.sheetName
    // console.log(' sheetName ', sheetName)
    var enumName = sheetName.substring(0, 1).toUpperCase() + sheetName.substring(1);
    // console.log(' enumName ', enumName)
    var enumObj = "export enum " + enumName + "Enum {\n\t%{data}\n}";
    var enums = "";
    var idIndex = this.sheet.getKeyIndex('ID')
    if (idIndex == -1) {
        idIndex = this.sheet.getKeyIndex('id')
        if (idIndex == -1) {
            idIndex = this.sheet.getKeyIndex('Id')
        }
    }
    var nameIndex = this.sheet.getKeyIndex('name')

    var descIndex = this.sheet.getKeyIndex('desc')

    // console.log(' idIndex ', idIndex, 'nameIndex ', nameIndex)
    if (idIndex >= 0 && nameIndex >= 0) {
        for (var j = Define.DATA_START; j < this.sheet.excleData.length; j++) {
            var curData = this.sheet.excleData[j];
            // console.log(' curData ', curData)
            // console.log(' curData.length ', curData.length)
            if (curData.length == 0 || curData[nameIndex] == undefined || curData[idIndex] == undefined) continue;
            // console.log(' curData[nameIndex] ', curData[nameIndex], '  curData[idIndex]  ', curData[idIndex])
            let desc = descIndex >= 0 ? curData[descIndex] : '';
            enums += curData[nameIndex] + " = " + curData[idIndex] + ",// " + desc + "\n\t";
            // console.log(' enums ', enums)
        }
    }
    // console.log(' enums ', enums)
    enumObj = enumObj.replace('%{data}', enums);
    this.enumDefine = enumObj;
    this.enumDefineName = enumName;

    // console.log('  this.enumDefine  ', this.enumDefine)

}

// TSParser.prototype.exportClass = function () {

//     let export_path = this.sheet.dir.class_path;
//     if (this.sheet.managerName) {
//         export_path = path.join(this.sheet.dir.class_path, this.sheet.managerName)
//     }
//     // console.log(' class_path ', export_path)

//     if (this.classObj) {
//         file_util.createDir(export_path)
//         file_util.writeFile(path.join(export_path, className + ".ts"), this.classObj);
//     }
// }

TSParser.prototype.parseClass = function (manager, template,functionTemplate) {
    if (!template) {
        console.warn('template is null ')
        return;
    }
    let sheetName = this.sheet.sheetName;
    var className = sheetName.substring(0, 1).toUpperCase() + sheetName.substring(1);
    let enumName = className + "Index"
    this.className = className;
    let TemplateName = 'Json' + className;
    let classObj = template.replace(/%ClassName%/g, className)
    classObj = classObj.replace('%SheetName%', this.sheet.sheetName)
    classObj = classObj.replace('%enum%', this.enumObj)

    classObj = classObj.replace('%TemplateName%', TemplateName)

    let exportStr = ''
    let keyArray = this.sheet.keyArray;
    let nameArray = this.sheet.nameArray;
    let typeArray = this.sheet.typeArray;
    for (var k = 0; k < keyArray.length; k++) {
        if (typeArray[k] != 'null') {
            let tname = Define.TS_TYPE[typeArray[k].trim()];
            if (!tname) {
                console.log(' tname is null type is ', typeArray[k].trim(), className, typeArray[k].trim())
                console.error(' tname is null type is ')
                tname = ''
            }
            var key = "// ";
            if (nameArray[k]) {
                key += nameArray[k].replace(/\n/g, '')
            }
            key += "\n\t"; //说明
            if (typeof (keyArray[k]) == "string") {
                let funcName = keyArray[k].substring(0, 1).toUpperCase() + keyArray[k].substring(1);
                key += 'get' + funcName + "(): " + tname + " {\n\t\t"; //getkeyArray[k](){
                // key += 'return this.getValue(' + this.enumName + 'Enum.' + this.keyArray[k] + ')\n\t';
                key += functionTemplate.replace(/%enumName%/g, enumName) + '\n\t';
                key = key.replace(/%property%/g, keyArray[k])
                // key += 'return this.cfg[' + enumName + '.' + keyArray[k] + '] ;\n\t';
                // key += 'return this.cfg.' + keyArray[k] + ' ;\n\t';
                key += '}\n\t'
                exportStr += key;
            }



        }
    }
    classObj = classObj.replace('%function%', exportStr)

    // console.log(' classObj ',classObj)
    // if (this.exportID) {
    //     var enumObj = "export class " + this.exportID + " {\n\t%{data}\n}";
    //     var enums = "";
    //     for (let key in this.keys) {
    //         enums += 'static ' + this.exportID + key + ' = ' + key + '\n\t'
    //     }
    //     enumObj = enumObj.replace('%{data}', enums);
    //     classObj = classObj.replace('%classid%', enumObj)
    // } else {
    //     classObj = classObj.replace('%classid%', '')
    // }

    this.classObj = classObj;

    //整合模型管理器信息
    if (manager) {
        var lowName = className.replace(className[0], className[0].toLowerCase());
        var proName = lowName + 'Mgr'
        manager[Define.IMPORT] += 'import { ' + className + ' } from "./' + className + '";\n'
        var head = 'protected ' + proName + ': ModelManager<' + className + '> = new ModelManager()'
        manager[Define.HEAD] += head + '\n\t';
        var init = 'this.' + proName + '.initWithData(cfw.xlsx.get(' + className + '.CLASS_NAME), ' +
            className + ')'
        manager[Define.INIT] += init + '\n\t\t';
        manager[Define.FUNCTION] += 'get' + className + '(id){ return this.' + proName + '.getByID(id,' + className + ')}\n\t\n\t'
        manager[Define.FUNCTION] += 'get' + className + 'List(){ return this.' + proName + '.getList()}\n\t\n\t'
    }


}
TSParser.prototype.parse = function (manager, template,functionTemplate) {
    this.parseEnum();
    this.parseEnumDefine();
    this.parseClass(manager, template,functionTemplate)
}

module.exports = TSParser; 